package org.agile2soft.nexoma.core.tapestry.pages.profile;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.agile2soft.nexoma.core.CoreConstants;
import org.agile2soft.nexoma.core.controller.ProfileController;
import org.agile2soft.nexoma.core.entity.InternalUser;
import org.agile2soft.nexoma.core.entity.Profile;
import org.agile2soft.nexoma.core.entity.ServiceOption;
import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Mixin;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.services.BeanModelSource;
import org.slf4j.Logger;

import br.com.arsmachina.tapestrycrud.base.BaseEditPage;
import br.com.arsmachina.tapestrycrud.hibernatevalidator.mixins.HibernateValidatorMixin;

public class EditProfile extends BaseEditPage<Profile,Integer> {

    public List<ServiceOption>getServiceOptions() {
        return new ArrayList(getObject().getServiceOptions());
    }

    public void setServiceOptions(List<ServiceOption>serviceOptions) {
        getObject().setServiceOptions(new HashSet<ServiceOption>(serviceOptions));
    }

    public ValueEncoder<ServiceOption> getServiceOptionsVE () {
        return getValueEncoder(ServiceOption.class);
    }

    public SelectModel getServiceOptionsSM () {
        return getSelectModelFactory().create(ServiceOption.class);
    }

    @Mixin
    @SuppressWarnings("unused")
    private HibernateValidatorMixin hibernateValidationMixin;

    @Inject
    private Logger logger;

    @OnEvent(value=EventConstants.SUCCESS)
    protected Object doSuccess() {
        return getFormZone();
    }


    @Override
    protected Object remove(Profile profile) {
        final int iUsersCount = profile.getInternalUsers().size();

        // Validate if the profile is still assigned
        if ( iUsersCount > 0  ) {

            logger.info(String.format("The Profile %s cannot be deleted because it is assigned to %s internal users ",profile.getName(),iUsersCount));

            setMessage(getMessages().get(CoreConstants.MESSAGE_ERROR_PROFILE_WITH_USERS));
            return getFormZone();
        }

        return super.remove(profile);

    }

    @Override
    protected void validateObject(Profile object, Form form) {

        for ( Profile profileBean : ((ProfileController)getController()).findByName(object.getName()) ) {
            if ( !profileBean.equals(object) ) {

                // Validate if the profile name is already in use
                addError("name",getMessages().get(CoreConstants.MESSAGE_ERROR_PROFILE_NAME));

            }
        }
        super.validateObject(object, form);
    }

    @Inject
    private BeanModelSource beanModelSource;

    public BeanModel<InternalUser> getInternalUserModel() {
        BeanModel<InternalUser> internalUserModel = beanModelSource.createDisplayModel(InternalUser.class, getMessages());
        internalUserModel.include("login","name","surname1","surname2");
        return internalUserModel;
    }
}

